package com.heaven1949.tmall.product.controller;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heaven1949.tmall.common.core.dto.ApiResult;
import com.heaven1949.tmall.common.web.controller.BaseController;
import com.heaven1949.tmall.product.dto.ProductPageQueryDTO;
import com.heaven1949.tmall.product.entity.ProductEntity;
import com.heaven1949.tmall.product.service.IProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 商品信息controller
 *
 * @author zhangjian
 * @date 2023-03-08 16:28:13
 */
@RestController
@RequestMapping("/api/product")
@RequiredArgsConstructor
public class ProductController extends BaseController {

    private final IProductService productService;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public ApiResult<IPage<ProductEntity>> page(ProductPageQueryDTO dto) {
        return success(productService.pageQuery(getPage(), dto));
    }

    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(10, 10,
                60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1024),
                new ThreadPoolExecutor.CallerRunsPolicy());
        for (int j = 0; j < 10; j++) {
            for (int i = 0; i < 100; i++) {
                int finalI = i;
                executorService.execute(() -> System.out.println((finalI + 1) + HttpUtil.get("http://localhost:3000/mock/18/products/page")));
            }
        }
        executorService.shutdown();
    }

    /**
     * 明细查询
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id:\\d+}")
    public ApiResult<ProductEntity> detail(@PathVariable Long id) {
        return success(productService.detail(id));
    }

}